/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package battleroom;

import com.jme3.asset.AssetManager;
import com.jme3.bounding.BoundingBox;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Geometry;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import com.jme3.scene.control.Control;
import com.jme3.scene.shape.Quad;

/**
 *
 * @author Adam
 */
public class HoloGate extends AbstractControl {
    Geometry holoField;
    Material holoFieldMaterial;
    AssetManager am;
    private float time = 0;
    
    public HoloGate(AssetManager am)
    {
        this.am = am;
    }
    
    @Override
    protected void controlUpdate(float tpf) {
        time += tpf;
        holoFieldMaterial.setFloat("time", (float)Math.sin(time));
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    public Control cloneForSpatial(Spatial spatial) {
        //throw new UnsupportedOperationException("Not supported yet.");
        return new HoloGate(am);
    }
    
    @Override
    public void setSpatial(Spatial spat)
    {
        super.setSpatial(spat);
        
        Geometry geom = (Geometry)spat;
        BoundingBox bound = (BoundingBox)geom.getModelBound();
        holoFieldMaterial = new Material(am, "MatDefs/HoloGate.j3md");
        holoFieldMaterial.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Off);
        holoFieldMaterial.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
        //holoFieldMaterial.setFloat("time", 0);
        holoField = new Geometry("holoField", new Quad(bound.getXExtent()*2, bound.getYExtent()*2));
        holoField.setMaterial(holoFieldMaterial);
        holoField.setLocalTranslation(spat.getLocalTranslation().subtract(new Vector3f(bound.getXExtent(), bound.getYExtent(), 0)));
        spat.getParent().attachChild(holoField);
    }
    
}
